package com.yw.smartcityforjava.fragment;

import android.os.Bundle;

import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;

import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.TextView;

import com.yw.smartcityforjava.R;
import com.yw.smartcityforjava.adapter.ServiceGridAdapter;
import com.yw.smartcityforjava.api.ICallback;
import com.yw.smartcityforjava.api.ServiceCreator;
import com.yw.smartcityforjava.api.allservices.ServicesResponse;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


public class ServiceFragment extends Fragment {
    private ListView serviceTypeLv;
    private GridView serviceGv;

    private List<ServicesResponse.RowsBean> allServices = new ArrayList<>();
    private Map<String, List<ServicesResponse.RowsBean>> serviceMap = new HashMap<>();

    public ServiceFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_service, container, false);
        Toolbar toolbar = root.findViewById(R.id.toolbar);
        TextView titleTv = toolbar.findViewById(R.id.titleTv);
        titleTv.setText("全部服务");

        serviceTypeLv = (ListView) root.findViewById(R.id.serviceTypeLv);
        serviceGv = (GridView) root.findViewById(R.id.serviceGv);

        serviceTypeLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ServiceGridAdapter myGridAdapter = new ServiceGridAdapter();
                if(position == 0){
                    myGridAdapter.setDatas(allServices);
                }else{
                    myGridAdapter.setDatas(serviceMap.get(serviceTypeLv.getAdapter().getItem(position)));
                }
                serviceGv.setAdapter(myGridAdapter);
            }
        });


        loadServices();
        return root;
    }



    private void loadServices(){
        ServiceCreator.getInstance().getApi().loadAllServices().enqueue(new ICallback<ServicesResponse>() {
            @Override
            public void onSuccess(ServicesResponse response) {
                allServices = response.getRows();
                Log.d("services", String.valueOf(allServices.size()));

                serviceMap = allServices.stream().filter(item-> !TextUtils.isEmpty(item.getServiceType())).collect(Collectors.groupingBy(ServicesResponse.RowsBean::getServiceType));
                List<String> serviceTypes = new ArrayList<>(serviceMap.keySet());
                serviceTypes.add(0, "全部服务");

                serviceTypeLv.setAdapter(new ArrayAdapter<String>(getContext(), R.layout.item_service_type, serviceTypes));

                //默认加载全部服务
                ServiceGridAdapter myGridAdapter = new ServiceGridAdapter();
                myGridAdapter.setDatas(allServices);
                serviceGv.setAdapter(myGridAdapter);
                //默认选中第一个
                serviceTypeLv.setItemChecked(0, true);
            }
        });
    }


}